package engine.res;

import math.Maths;

public class Animation 
{
    
    private static final int DIR_FORW = 1;
    private static final int DIR_REVR = -1;
    
    private String fileName;
    public String getFileName() 
    {
        return fileName;
    }
    
    private Bitmap[] frames;
    private int nFrames;
    
    private int currentFrame;
    public int getCurrentFrameIndex() 
    {
        return currentFrame;
    }
    
    private int direction;
    
    public Animation(String filename, int numFrames) 
    {
        fileName = filename;
        nFrames = numFrames;
        frames = Res.loader().tryLoadAnimation(fileName, nFrames);
        currentFrame = 0;
        direction = DIR_FORW;
    }
    
    public void update() 
    {
        currentFrame = Maths.modi(currentFrame + direction, nFrames);
    }
    
    public void changeDirecion() 
    {
        if (direction == DIR_FORW) 
        {
            direction = DIR_REVR;
        } 
        else /* (direction == DIR_REVR) */ 
        {
            direction = DIR_FORW;
        }
    }
    
    public Bitmap getFrameAt(int index) 
    {
//        int ind = Maths.modi(index, nFrames); // Similar to GL_REPEAT
        int ind = Maths.capi(index, 0, nFrames - 1); // Similar to GL_CLAMP
        return frames[ind];
    }
    
    public Bitmap getCurrentFrame() 
    {
        return this.getFrameAt(currentFrame);
    }
    
    public String toString() 
    {
        String s = "";
        s += "ANIMATION:";
        s += "\n   Filename: " + fileName;
        s += "\n   nFrames: " + nFrames;
        s += "\n   currentFrame: " + currentFrame;
        s += "\n   direction: " + (direction == DIR_FORW ? "FORW" : "REVR");
        return s;
    }
    
}
